package base.util;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import base.util.safe.CipherUtil;
import mt.Const;

/**
 * 配置文件config.properties所有配置
 */
public class PropertiesContent {

    public static Logger log = Logger.getLogger(PropertiesContent.class);
    public static Map<String, Object> config = new HashMap<String, Object>();
    public static Properties properties = new Properties();
    public static PropertiesContent me = new PropertiesContent();

    static {

        File rootPath = new File(PropertiesContent.class.getResource("/").getPath());
        File[] cfgFiles = rootPath.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith("properties") && !name.contains("log4j");
            }
        });

        if (cfgFiles == null || cfgFiles.length < 1) {
            throw new RuntimeException("加载系统配置失败,请检查配置路径是否在Classpath根目录下，或命名是否正确！");
        }

        for (File cfgFile : cfgFiles) {
            ResourceBundle rb = ResourceBundle
                    .getBundle(StringUtils.substringBefore(cfgFile.getName(), Const.DOT_SEPARATOR));
            Enumeration<String> cfgs = rb.getKeys();
            while (cfgs.hasMoreElements()) {
                String key = cfgs.nextElement();
                String val = rb.getString(key);
                if (key.contains("mail.password") == true) {
                    try {
                        config.put(key, CipherUtil.decryptData(val));
                    } catch (Exception e) {
                        log.error("对参数解密异常", e);
                    }
                } else {
                    config.put(key, val);
                }
            }
            properties.putAll(config);
        }
    }

    public static String get(String key) {
        return (String) config.get(key);
    }

    public static String get(String key, String def) {
        String v = get(key);
        if (v == null) {
            return def;
        } else {
            return v;
        }
    }

    public static Object getObj(String key) {
        return config.get(key);
    }

    public static Boolean getToBool(String key, Boolean def) {
        try {
            return Boolean.valueOf((String) config.get(key));
        } catch (Exception e) {
            return def;
        }
    }

    public static Integer getToInteger(String key, Integer def) {
        try {
            return Integer.valueOf((String) config.get(key));
        } catch (Exception e) {
            return def;
        }
    }

    public static Long getToLong(String key, Long def) {
        try {
            return Long.valueOf((String) config.get(key));
        } catch (Exception e) {
            return def;
        }
    }
}
